From 96f9b51248351231b624eeed8853dfebd8e25ede Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Thu, 5 Dec 2019 21:19:32 -0600 Subject: [PATCH] Add literal_column_headings setting --- examples/development.ini | 11 +++++++++++ examples/pgwui.ini | 11 +++++++++++ src/pgwui_server/__init__.py | 21 +++++++++++++++++++++ tests/test___init__.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) diff --git a/examples/development.ini b/examples/development.ini index 4ea2ce8..9d55a8e 100644 --- a/examples/development.ini +++ b/examples/development.ini @@ -77,6 +77,17 @@ pgwui.dry_run = False # vulnerabilties. Validation is on by default. pgwui.validate_hmac = False +# Take uploaded column headings literally? +# The available choices are: +# on The file's column headings, as typed, are the table's column names. +# off The file's column headings are case-insensitive. +# ask Put a "Take Column Headings Literally" checkbox on the upload screen. +# Optional setting. The default is "off". +# +# Caution: Non-ASCII column names, particularly in the Turkish locale, +# are not guaranteed to be case-insensitive. +pgwui.literal_column_headings = off + # # Pyramid configuration diff --git a/examples/pgwui.ini b/examples/pgwui.ini index 552dd36..52b9109 100644 --- a/examples/pgwui.ini +++ b/examples/pgwui.ini @@ -76,6 +76,17 @@ pgwui.dry_run = False # vulnerabilties. Validation is on by default. # pgwui.validate_hmac = True +# Take uploaded column headings literally? +# The available choices are: +# on The file's column headings, as typed, are the table's column names. +# off The file's column headings are case-insensitive. +# ask Put a "Take Column Headings Literally" checkbox on the upload screen. +# Optional setting. The default is "off". +# +# Caution: Non-ASCII column names, particularly in the Turkish locale, +# are not guaranteed to be case-insensitive. +pgwui.literal_column_headings = off + # # Pyramid configuration diff --git a/src/pgwui_server/__init__.py b/src/pgwui_server/__init__.py index 68e43e8..0f39fc1 100644 --- a/src/pgwui_server/__init__.py +++ b/src/pgwui_server/__init__.py @@ -36,6 +36,7 @@ SETTINGS = set( 'route_prefix', 'routes', 'validate_hmac', + 'literal_column_headings', ]) # Required length of HMAC value @@ -66,6 +67,13 @@ class NotBooleanSettingError(Error): .format(key)) +class BadLiteralColumnHeadingsError(Error): + def __init__(self, value): + super().__init__( + 'The "pgwui.literal_column_headings" PGWUI setting must be' + '"on", "off", "ask", or not present') + + class BadHMACError(Error): pass @@ -125,6 +133,9 @@ def validate_setting_values(settings): # validate_hmac boolean_setting('pgwui.validate_hmac', settings) + # literal_column_headings + validate_literal_column_headings(settings) + def do_validate_hmac(settings): '''True unless the user has specificly rejected hmac validation @@ -145,6 +156,16 @@ def validate_hmac(settings): raise HMACLengthError() +def validate_literal_column_headings(settings): + '''Make sure the values are those allowed + ''' + value = settings.get('pgwui.literal_column_headings') + if value is None: + return + if value not in ('on', 'off', 'ask'): + raise BadLiteralColumnHeadingsError(value) + + def validate_settings(settings): '''Be sure all settings validate ''' diff --git a/tests/test___init__.py b/tests/test___init__.py index b7588e8..ca50405 100644 --- a/tests/test___init__.py +++ b/tests/test___init__.py @@ -197,6 +197,38 @@ def test_validate_hmac_length(monkeypatch): pgwui_server_init.validate_hmac({'session.secret': ''}) +# validate_literal_column_headings() + +def test_validate_literal_column_headings_nosetting(): + '''No error is raised when there's no setting''' + pgwui_server_init.validate_literal_column_headings({}) + + +def test_validate_literal_column_headings_on(): + '''No error is raised when the setting is "on"''' + pgwui_server_init.validate_literal_column_headings( + {'pgwui.literal_column_headings': 'on'}) + + +def test_validate_literal_column_headings_off(): + '''No error is raised when the setting is "off"''' + pgwui_server_init.validate_literal_column_headings( + {'pgwui.literal_column_headings': 'off'}) + + +def test_validate_literal_column_headings_ask(): + '''No error is raised when the setting is "ask"''' + pgwui_server_init.validate_literal_column_headings( + {'pgwui.literal_column_headings': 'ask'}) + + +def test_validate_literal_column_headings_bad(): + '''Raises an error when given a bad value''' + with pytest.raises(pgwui_server_init.BadLiteralColumnHeadingsError): + pgwui_server_init.validate_literal_column_headings( + {'pgwui.literal_column_headings': 'bad'}) + + # validate_settings() def test_validate_settings(monkeypatch): -- 2.34.1